import { createRouter, createWebHashHistory } from "vue-router";

import { getToken } from "utils/auth";

const routes = [
  {
    path: "/",
    name:"home",
    component: () => import("../pages/home/home.vue"),
  },
  {
    path: "/register",
    name:"register",
    component: () => import("../pages/register/register.vue"),
  },
  {
    path: "/login",
    name:"login",
    component: () => import("../pages/login/login.vue"),
  },
  {
    path: "/write-notes",
    name:"writeNotes",
    component: () => import("../pages/write-notes/write-notes.vue"),
  },
  {
    path: "/note-detail",
    name:"noteDetail",
    component: () => import("../pages/note-detail/note-detail.vue"),
  },
  {
    path: "/video-player",
    name:"videoPlayer",
    component: () => import("../pages/video-player/video-player.vue"),
  },
  
  
];

const router = createRouter({
  history: createWebHashHistory(), // hash路由模式
  // history: createWebHistory(),  // history路由模式
  routes,
});

const whiteList = ["/login","/register"]; // no redirect whitelist

router.beforeEach(async (to, from, next) => {
  const hasToken = getToken();

  if (hasToken) {
    if (to.path === "/login") {
      next({ path: "/" });
    }else{
      next()
    }
  } else {
    /* has no token*/
    if (whiteList.indexOf(to.path) !== -1) {
      // in the free login whitelist, go directly
      next();
    } else {
      next(`/login?redirect=${to.fullPath}`);
    }
  }
});

export default router;
